package com.taptech.doufu.util.image;
import android.content.Intent;
import android.widget.Toast;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.taobao.weex.bridge.JSCallback;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.GlideEngine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Wyatt on 2018/1/19/019.
 */

public class ImagePickUtil {
    public static int IMAGE_PICKER_REQUEST = 1;
    public static int CROP_TYPE_CIRCLE = 1;

    public interface OnPickImageListen{
        void onPickImage(List<LocalMedia> paths);
    }

    public static void pickImage(final DiaobaoBaseActivity activity, int count,boolean isCrop,
                                 final JSCallback callback){
        pickImage(activity,count,isCrop,CROP_TYPE_CIRCLE,0,0,callback);
    }


    public static void pickImage(final DiaobaoBaseActivity activity, int count,boolean isCrop,int cropType,
                               int width, int height, final JSCallback callback) {
        if (width == 0) {
            width = 1;
        }
        if (height == 0) {
            height = 1;
        }
        boolean circleDimmedLayer = false;
        if (cropType == CROP_TYPE_CIRCLE) {
            circleDimmedLayer = true;
        }
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .circleDimmedLayer(circleDimmedLayer)
                .maxSelectNum(count)
                .imageSpanCount(4)// 每行显示个数
                .isPreviewImage(true)
                .isUseCustomCamera(false)
                .isCamera(false)
                .isMaxSelectEnabledMask(true)
                .isEnableCrop(isCrop)// 是否裁剪
                .withAspectRatio(width,height)
                .freeStyleCropEnabled(true)
                .showCropFrame(!circleDimmedLayer)
                .showCropGrid(!circleDimmedLayer)
                .cutOutQuality(90)// 裁剪输出质量 默认100
                .minimumCompressSize(100)// 小于多少kb的图片不压缩
                .isGif(!isCrop)// 是否显示gif图片
                .imageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> paths) {
                        // onResult Callback
                        ArrayList images = new ArrayList();
                        if (paths instanceof ArrayList ){
                            int count = paths.size();
                            for (int i = 0; i< count ; i++){
                                String path = "";
                                if (paths.get(i).isCut()) {
                                    path = paths.get(i).getCutPath();
                                }else{
                                    path = paths.get(i).getRealPath();
                                }
                                HashMap imageInfo = new HashMap();
                                imageInfo.put("url",path);
                                imageInfo.put("width",paths.get(i).getWidth());
                                imageInfo.put("height",paths.get(i).getHeight());
                                images.add(imageInfo);
                            }
                        }
                        if (callback != null){
                            HashMap res = new HashMap();
                            res.put("images",images);
                            callback.invoke(res);
                        }
                    }

                    @Override
                    public void onCancel() {
                        // onCancel Callback
                    }
                });
    }




    public static void pickCamera(final DiaobaoBaseActivity activity, int count,boolean isCrop,int cropType,
                                 int width, int height, final JSCallback callback) {
        if (width == 0) {
            width = 1;
        }
        if (height == 0) {
            height = 1;
        }
        boolean circleDimmedLayer = false;
        if (cropType == CROP_TYPE_CIRCLE) {
            circleDimmedLayer = true;
        }
        PictureSelector.create(activity)
                .openCamera(PictureMimeType.ofImage())
                .isPreviewImage(true)
                .isUseCustomCamera(false)
                .isMaxSelectEnabledMask(true)
                .isEnableCrop(isCrop)// 是否裁剪
                .withAspectRatio(width,height)
                .freeStyleCropEnabled(true)
                .showCropFrame(!circleDimmedLayer)
                .showCropGrid(!circleDimmedLayer)
                .cutOutQuality(90)// 裁剪输出质量 默认100
                .minimumCompressSize(100)// 小于多少kb的图片不压缩
                .isGif(!isCrop)// 是否显示gif图片
                .imageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> paths) {
                        // onResult Callback
                        ArrayList images = new ArrayList();
                        if (paths instanceof ArrayList ){
                            int count = paths.size();
                            for (int i = 0; i< count ; i++){
                                String path = "";
                                if (paths.get(i).isCut()) {
                                    path = paths.get(i).getCutPath();
                                }else{
                                    path = paths.get(i).getRealPath();
                                }
                                HashMap imageInfo = new HashMap();
                                imageInfo.put("url",path);
                                imageInfo.put("width",paths.get(i).getWidth());
                                imageInfo.put("height",paths.get(i).getHeight());
                                images.add(imageInfo);
                            }
                        }
                        if (callback != null){
                            HashMap res = new HashMap();
                            res.put("images",images);
                            callback.invoke(res);
                        }
                    }

                    @Override
                    public void onCancel() {
                        // onCancel Callback
                    }
                });
    }

    public static void pickImage(final DiaobaoBaseActivity activity, int count,boolean isCrop,int cropType,
                                 int width, int height, int fwidth,int fheight,final OnPickImageListen callback) {
        if (width == 0) {
            width = 1;
        }
        if (height == 0) {
            height = 1;
        }
        boolean circleDimmedLayer = false;
        if (cropType == CROP_TYPE_CIRCLE) {
            circleDimmedLayer = true;
        }
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .circleDimmedLayer(circleDimmedLayer)
                .maxSelectNum(count)
                .imageSpanCount(4)// 每行显示个数
                .isPreviewImage(true)
                .isCamera(true)
                .isMaxSelectEnabledMask(true)
                .isEnableCrop(isCrop)// 是否裁剪
                .withAspectRatio(width,height)
                .freeStyleCropEnabled(true)
                .showCropFrame(!circleDimmedLayer)
                .showCropGrid(!circleDimmedLayer)
                .cutOutQuality(90)// 裁剪输出质量 默认100
                .minimumCompressSize(100)// 小于多少kb的图片不压缩
                .isGif(!isCrop)// 是否显示gif图片
                .imageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> paths) {
                        // onResult Callback
                        callback.onPickImage(paths);
                    }

                    @Override
                    public void onCancel() {
                        // onCancel Callback
                    }
                });
    }

}
